﻿using System.Collections.Generic;
using System.Linq;
using Nxg.Blog.Core.Documents;

namespace Nxg.Blog.Core.ViewProjections.Admin {
    public class AllBlogCommentsBindingModel {
        public AllBlogCommentsBindingModel () {
            Page = 1;
            Take = 20;
        }

        public int Page { get; set; }

        public int Take { get; set; }
    }

    public class AllBlogCommentsViewModel {
        public IEnumerable<BlogComment> Comments { get; set; }

        public bool HasNextPage { get; set; }

        public bool HasPrevPage {
            get {
                return Page > 1;
            }
        }

        public int Page { get; set; }
    }

    public class BlogCommentsViewProjection : IViewProjection<AllBlogCommentsBindingModel, AllBlogCommentsViewModel> {
        private readonly DataContext _db;

        public BlogCommentsViewProjection (DataContext db) {
            _db = db;
        }

        public AllBlogCommentsViewModel Project (AllBlogCommentsBindingModel input) {
            var skip = (input.Page - 1) * input.Take;
            var comments = _db.Comment.OrderByDescending (x => x.CreatedTime).Skip (skip).Take (input.Take + 1).ToList ();
            var pagedComments = comments.Take (input.Take);
            var hasNextPage = comments.Count > input.Take;

            return new AllBlogCommentsViewModel {
                Comments = pagedComments,
                    Page = input.Page,
                    HasNextPage = hasNextPage
            };
        }
    }
}